package bb.lanxing.lib.devices.utils;

import android.content.Context;
import android.content.SharedPreferences;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DeviceVersionHelper {
    private static final String KEY_DEVICE_LAST_CHECK_TIME = "key_device_last_check_time_";
    private static final String KEY_DEVICE_LOCAL_FIRMWARE_VERSION = "key_device_firmware_version_";
    private static final String KEY_DEVICE_LOCAL_FIRMWARE_VERSION_DESC = "key_device_firmware_version_desc_";
    private static final String KEY_DEVICE_LOCAL_HW = "key_device_hw_";
    private static final String KEY_DEVICE_LOCAL_MODEL = "key_device_model_";
    private static final String KEY_DEVICE_LOCAL_SOFTWARE_VERSION = "key_device_software_version_";
    private static final String KEY_DEVICE_NEW_FIRMWARE_PATH = "key_device_firmware_path_";
    private static final String KEY_DEVICE_NEW_FIRMWARE_VERSION = "key_device_new_firmware_version_";
    private static final String KEY_DEVICE_NEW_FIRMWARE_VERSION_DESC = "key_device_new_firmware_version_desc_";
    private static final String KEY_DEVICE_NEW_FIRMWARE_VERSION_URL = "key_device_new_firmware_version_url_";
    private static final String KEY_LAST_SPRINT_SUPPORTED_VERSION = "key_last_sprint_supported_version";

    private static SharedPreferences preferences(Context context) {
        return context.getSharedPreferences("device_version", 0);
    }

    public static int getLastSprintSupportedVersion(Context context) {
        return preferences(context).getInt(KEY_LAST_SPRINT_SUPPORTED_VERSION, 1);
    }

    public static void setLastSprintSuppportedVersion(Context context, int i) {
        preferences(context).edit().putInt(KEY_LAST_SPRINT_SUPPORTED_VERSION, i).apply();
    }

    public static void setFirmwarePath(Context context, int i, String str) {
        SharedPreferences.Editor edit = preferences(context).edit();
        edit.putString(KEY_DEVICE_NEW_FIRMWARE_PATH + i, str).apply();
    }

    public static String getFirmwarePath(Context context, int i) {
        SharedPreferences preferences = preferences(context);
        return preferences.getString(KEY_DEVICE_NEW_FIRMWARE_PATH + i, null);
    }

    public static String getLocalVersion(Context context, String str) {
        SharedPreferences preferences = preferences(context);
        return preferences.getString(KEY_DEVICE_LOCAL_FIRMWARE_VERSION + str, null);
    }

    public static void setLocalVersion(Context context, String str, String str2) {
        SharedPreferences.Editor edit = preferences(context).edit();
        edit.putString(KEY_DEVICE_LOCAL_FIRMWARE_VERSION + str, str2).apply();
    }

    public static void setLocalSoftwareVersion(Context context, String str, String str2) {
        SharedPreferences.Editor edit = preferences(context).edit();
        edit.putString(KEY_DEVICE_LOCAL_SOFTWARE_VERSION + str, str2).apply();
    }

    public static void setHwVersion(Context context, String str, String str2) {
        SharedPreferences.Editor edit = preferences(context).edit();
        edit.putString(KEY_DEVICE_LOCAL_HW + str, str2).apply();
    }

    public static String getHwVersion(Context context, String str) {
        SharedPreferences preferences = preferences(context);
        return preferences.getString(KEY_DEVICE_LOCAL_HW + str, null);
    }

    public static void setModelVersion(Context context, String str, String str2) {
        SharedPreferences.Editor edit = preferences(context).edit();
        edit.putString(KEY_DEVICE_LOCAL_MODEL + str, str2).apply();
    }

    public static String getModelVersion(Context context, String str) {
        SharedPreferences preferences = preferences(context);
        return preferences.getString(KEY_DEVICE_LOCAL_MODEL + str, null);
    }

    public static String getLocalSoftwareVersion(Context context, String str) {
        SharedPreferences preferences = preferences(context);
        return preferences.getString(KEY_DEVICE_LOCAL_SOFTWARE_VERSION + str, null);
    }

    public static String getServerVersion(Context context, int i) {
        SharedPreferences preferences = preferences(context);
        return preferences.getString(KEY_DEVICE_NEW_FIRMWARE_VERSION + i, null);
    }

    public static void setServerVersion(Context context, int i, String str) {
        SharedPreferences.Editor edit = preferences(context).edit();
        edit.putString(KEY_DEVICE_NEW_FIRMWARE_VERSION + i, str).apply();
    }

    public static void setUrl(Context context, int i, String str) {
        SharedPreferences.Editor edit = preferences(context).edit();
        edit.putString(KEY_DEVICE_NEW_FIRMWARE_VERSION_URL + i, str).apply();
    }

    public static boolean isFirstLessThanSecond(Context context, String str, int i) {
        if (context == null || str == null) {
            return false;
        }
        return isFirstLessThanSecond(getLocalVersion(context, str), getServerVersion(context, i));
    }

    public static boolean isFirstLessThanSecond(String str, String str2) {
        if (str != null && str2 != null) {
            String replace = str.trim().toLowerCase().replace("v", "");
            String replace2 = str2.trim().toLowerCase().replace("v", "");
            String[] split = replace.split("\\.");
            String[] split2 = replace2.split("\\.");
            try {
                int parseInt = split.length >= 1 ? Integer.parseInt(split[0]) : 0;
                int parseInt2 = split2.length >= 1 ? Integer.parseInt(split2[0]) : 0;
                int parseInt3 = split.length >= 2 ? Integer.parseInt(split[1]) : 0;
                int parseInt4 = split2.length >= 2 ? Integer.parseInt(split2[1]) : 0;
                int parseInt5 = split.length >= 3 ? Integer.parseInt(split[2]) : 0;
                int parseInt6 = split2.length >= 3 ? Integer.parseInt(split2[2]) : 0;
                if (parseInt2 > parseInt) {
                    return true;
                }
                if (parseInt2 < parseInt || parseInt4 <= parseInt3) {
                    return parseInt2 >= parseInt && parseInt4 >= parseInt3 && parseInt6 > parseInt5;
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static void refreshLastCheckTime(Context context, int i) {
        SharedPreferences.Editor edit = preferences(context).edit();
        edit.putLong(KEY_DEVICE_LAST_CHECK_TIME + i, System.currentTimeMillis()).apply();
    }

    public static boolean shouldUpdateVerionInfo(Context context, int i, long j) {
        SharedPreferences preferences = preferences(context);
        StringBuilder sb = new StringBuilder();
        sb.append(KEY_DEVICE_LAST_CHECK_TIME);
        sb.append(i);
        return System.currentTimeMillis() - preferences.getLong(sb.toString(), 0L) > j;
    }

    public static String getServerVersionDesc(Context context, int i) {
        SharedPreferences preferences = preferences(context);
        return preferences.getString(KEY_DEVICE_NEW_FIRMWARE_VERSION_DESC + i, null);
    }

    public static String getUrl(Context context, int i) {
        SharedPreferences preferences = preferences(context);
        return preferences.getString(KEY_DEVICE_NEW_FIRMWARE_VERSION_URL + i, null);
    }

    public static void setServerVersionDesc(Context context, int i, String str) {
        SharedPreferences.Editor edit = preferences(context).edit();
        edit.putString(KEY_DEVICE_NEW_FIRMWARE_VERSION_DESC + i, str).apply();
    }

    public static boolean isNeedUpdate(Context context, String str, int i) {
        if (context == null || str == null) {
            return false;
        }
        return isNeedUpdate(getLocalVersion(context, str), getServerVersion(context, i));
    }

    public static boolean isNeedUpdate(String str, String str2) {
//        boolean z;
//        boolean z2 = false;
        if (str == null || str2 == null || str.equals("") || str2.equals("")) {
            return false;
        }
        String pureVersion = getPureVersion(str);
        String pureVersion2 = getPureVersion(str2);
        String[] split = pureVersion.split("\\.");
        String[] split2 = pureVersion2.split("\\.");
//        try {
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        if (Integer.parseInt(split[0]) < Integer.parseInt(split2[0])) {
//            z = true;
//            if (!z) {
//                try {
//                } catch (Exception e2) {
//                    e2.printStackTrace();
//                    return z;
//                }
//            }
//            z2 = true;
            return true;
        }
//        z = false;
//        if (!z) {
//        }
//        z2 = true;
        return false;
    }

    public static String getPureVersion(String str) {
        String group;
        Matcher matcher = Pattern.compile("(\\d+\\.\\d+)").matcher(str);
        if (matcher.find()) {
            if (matcher.group(1) == null) {
                return "";
            }
            group = matcher.group(1);
        } else {
            Matcher matcher2 = Pattern.compile("(\\d+)").matcher("0");
            if (!matcher2.find() || matcher2.group(1) == null) {
                return "";
            }
            group = matcher2.group(1);
        }
        return group;
    }

    public static void updateLocalVersionToServerVersion(Context context, String str, int i) {
        SharedPreferences preferences = preferences(context);
        String string = preferences.getString(KEY_DEVICE_LOCAL_FIRMWARE_VERSION + str, "");
        String string2 = preferences.getString(KEY_DEVICE_NEW_FIRMWARE_VERSION + i, string);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString(KEY_DEVICE_LOCAL_FIRMWARE_VERSION + str, string2).apply();
    }
}
